package org.dandelion.ruler;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.dandelion.R;

public class RulerActivity extends AppCompatActivity {
    private static final String TAG = "tv";
    private ImageView imageView;
    private TextView rst;
    private Button record;
    private Button getRecord;
    private ImageView rulerbg;
    private int sx;
    private int sy;
    private float CM = 0;
    private SharedPreferences sharedPreferences = null;
    private SharedPreferences.Editor editor = null;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ruler);

        imageView = findViewById(R.id.iv);
        imageView.setTag(TAG);
        rst = findViewById(R.id.rst);
        record = findViewById(R.id.record);
        getRecord = findViewById(R.id.getRecord);
        rulerbg = findViewById(R.id.rulerbg);

        ViewGroup.LayoutParams layoutParams = rulerbg.getLayoutParams();
        layoutParams.height = getPX(124);
        rulerbg.setLayoutParams(layoutParams);

        imageView.setOnTouchListener((v, event) -> {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    sx = (int) event.getRawX();
                    sy = (int) event.getRawY();
                    break;
                }

                case MotionEvent.ACTION_MOVE: {
                    int x = (int) event.getRawX();
                    int y = (int) event.getRawY();
                    int dx = x - sx;
                    int dy = y - sy;
                    int left = imageView.getLeft();
                    int right = imageView.getRight();
                    int top = imageView.getTop();
                    int bottom = imageView.getBottom();
                    imageView.layout(left, top + dy, right, bottom + dy);
                    sx = (int) event.getRawX();
                    sy = (int) event.getRawY();
                    break;
                }

                case MotionEvent.ACTION_UP: {
                    int lasty = imageView.getTop();
                    int lastx = imageView.getLeft();
                    Log.d("lyq", "dp:" + lasty);
                    CM = getCM(lasty);
                    Log.d("lyq", "CM:" + CM);
                    showRst(CM);
                    break;
                }

            }
            return true;
        });

        sharedPreferences = getSharedPreferences("len", MODE_PRIVATE);
        editor = sharedPreferences.edit();
        editor.putInt("count", 0);
        editor.apply();

        record.setOnClickListener(view -> {
            int count = sharedPreferences.getInt("count", 0);
            count += 1;
            editor.putFloat("r" + count, CM);
            editor.putInt("count", count);
            editor.commit();
            Toast.makeText(RulerActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
        });

        getRecord.setOnClickListener(view -> {
            Intent intent = new Intent(RulerActivity.this, ResultActivity.class);
            startActivity(intent);
        });
    }

    public int getPX(int mm) {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        float px = mm * dm.ydpi * (1.0f / 25.4f);
        return (int) px;
    }

    public float getCM(int len) {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        float mm = len / dm.ydpi / (1.0f / 25.4f);
        return mm / 10f;

    }

    public void showRst(float len) {
        String strRst = "CM";
        String s = String.format("%.1f", len);
        Log.d("lyq", s);
        strRst = s + strRst;
        rst.setText(strRst);
    }
}